clear all
*cap log close
set more off
set seed 413


// outcomes plots --------------------------------------------------------
local Q = 2
local nameout = "outcomes"

// setup data -------------------------
use "${temp}/data_extrap", clear 
replace n_cite2  = . if year > 2015
replace n_speed2 = . if year > 2015 
replace n_crash2 = . if year > 2015

/*
gen D  = harsh
gen Y1 = cite_ny1 
gen Y2 = speed_ny1 
gen Y3 = crashany_ny1
gen Y4 = n_cite2 
gen Y5 = n_speed2 
gen Y6 = n_crash2
gen Y7 = contest
*/
forval q = 1/`Q' {
	gen Z_`q' = Z^`q'
}

tempfile temp
save    `temp'
// ------------------------------------



// loop over outcomes -----------------
* for graph naming 
local name1 = "cite"
local name2 = "speed"
local name3 = "crash"
local name4 = "ncite"
local name5 = "nspeed"
local name6 = "ncrash"
local name7 = "contest"

local k = 1
foreach y in cite_ny1 speed_ny1 crash_ny1 n_cite2 n_speed2 n_crash2 contest {

	// store estimates (poly) -----
	import delimited using "${est}/outcomes_q2.csv", clear
	keep if yvar == "`y'"
	replace par = trim(upper(par))
	keep if par == "Y0"|par=="Y1"
	gen Z = 0
	replace Z = 1 if par=="Y1"

	keep Z est ub lb  
	gen flag_poly = 1
	tempfile est1
	save    `est1'


	// store estimates (bins) --------
	import delimited using "${est}/outcomes_b5.csv", clear
	keep if yvar == "`y'"
	replace par = trim(upper(par))
	keep if par == "Y0"|par=="Y1"
	gen Z = . 
	replace Z = 0.025 if par=="Y0"
	replace Z = 0.975 if par=="Y1"
	keep Z est ub lb  
	gen flag_bin = 1
	tempfile est2
	save    `est2'


	// estimate quadratic fit ---------
	use `temp', clear 
	reghdfe `y' Z_*, absorb(totfe)
	local b0 = _b[_cons]
	forval q = 1/`Q' {
		local b`q' = _b[Z_`q']
	}

	// estimate binscatter ---------
	use `temp', clear
	sort Z 
	binsreg `y' Z, absorb(totfe) binspos(es) savedata(bs) replace 


	// compile together ------------
	use bs, clear 
	rename dots_x Z 
	rename dots_fit est 
	gen flag_bs = 1 
	rm bs.dta

	qui append using `est1'
	qui append using `est2'


	// generate quadratic fit for graphing ---------
	sort Z
	gen fit = `b0'
	forval q = 1/`Q' {
		replace fit = fit + `b`q''*Z^`q'
	}

	// Build plot -------------
	if `k'<7 {
	sort Z
	#delimit ;
	scatter est Z if flag_bs==1, msymbol(Oh) mcolor(dknavy) msize(medium) || 
	line fit Z, lcolor(dkgreen) ||
	scatter est Z if flag_poly==1, mcolor(dkgreen) msize(med) ||
	rspike ub lb Z if flag_poly==1, lcolor(dkgreen) ||
	scatter est Z if flag_bin==1, msymbol(D) msize(medium) mcolor(cranberry)
	graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) 
	ylab(,nogrid) xlab(,nogrid)
	xtitle("Officer Stringency") ytitle("")
	legend(ring(0) cols(1) pos(2) order(1 2 5) region(lstyle(border))
	lab(1 "Binscatter") lab(2 "Quadratic Fit") lab(5 "Tail Mean: 5%")) ;
	#delimit cr
	graph export "${out}/apx_extrap/`nameout'_`name`k''.pdf", replace 
	}
	if `k'==7 {
	sort Z
	#delimit ;
	scatter est Z if flag_bs==1, msymbol(Oh) mcolor(dknavy) msize(medium) || 
	line fit Z, lcolor(dkgreen) ||
	scatter est Z if flag_poly==1, mcolor(dkgreen) msize(med) ||
	rspike ub lb Z if flag_poly==1, lcolor(dkgreen) ||
	scatter est Z if flag_bin==1, msymbol(D) msize(medium) mcolor(cranberry)
	graphregion(color(white)) plotregion(lcolor(black) lwidth(medthin)) 
	ylab(,nogrid) xlab(,nogrid)
	xtitle("Officer Stringency") ytitle("")
	legend(ring(0) cols(1) pos(10) order(1 2 5) region(lstyle(border))
	lab(1 "Binscatter") lab(2 "Quadratic Fit") lab(5 "Tail Mean: 5%")) ;
	#delimit cr
	graph export "${out}/apx_extrap/`nameout'_`name`k''.pdf", replace 
}

local ++k 
}
// end of outcome plots ---------------------------------------------------------





// outcomes table ----------------------------------------------------------------
// main text: quadratic extrapolation, no contesting 

// initialize LaTeX Table ---------------------
capture erase "${out}/main/table_outcomes.tex"
file open fh using "${out}/main/table_outcomes.tex", write replace

file write fh ///	
	"&\multicolumn{4}{c}{\$Y_0\$} && \multicolumn{4}{c}{\$Y_1 - Y_0\$}\\" _n /// 
	"\cmidrule(l){2-5} \cmidrule(l){7-10}" _n ///
	"& (1) & (2) & (3) & (4) && (5) & (6) & (7) & (8)\\" _n ///
	"& All & \$D=1\$ & \$D=0\$ & Diff && All & \$D=1\$ & \$D=0\$ & Diff \\" _n ///
	"&     &         &         &      && (ATE) & (ATT) & (ATU) &  \\" _n ///
	"\hline" _n
	

// first panel: any in the next year ---------------
file write fh ///
	"\multicolumn{6}{l}{\textit{\underline{Panel A: Any offense in following year}}}  \\"  _n ///
	
	
// Prep for reading loop -------------
local y1    = "cite_ny1"
local glab1 = "Any Offense"
local y2    = "speed_ny1"
local glab2 = "Speeding"
local y3    = "crash_ny1"
local glab3 = "Crash"

forval q = 1/3 {
	
	import delimited using "${est}/outcomes_q2.csv", clear
	keep if yvar == "`y`q''"
	replace par = trim(upper(par))
	local label = "`glab`q''"

	local k = 1
	foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
		qui summ est if par == "`p'" 
		local m`k' = "`:di %4.3f `r(mean)''"
		qui summ se  if par == "`p'"
		local s`k' = "(`:di %4.3f `r(mean)'')"
		local ++k 
	}

	*di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
	*di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

	file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
	file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 
}	


// second panel: counts over next two years ----------
file write fh "[1em]" _n 
file write fh ///
	"\multicolumn{6}{l}{\textit{\underline{Panel B: Number of offenses in following two years}}}  \\"  _n ///
	

local y1    = "n_cite2"
local glab1 = "Any Offense"
local y2    = "n_speed2"
local glab2 = "Speeding"
local y3    = "n_crash2"
local glab3 = "Crash"


forval q = 1/3 {
	
	import delimited using "${est}/outcomes_q2.csv", clear
	keep if yvar == "`y`q''"
	replace par = trim(upper(par))
	local label = "`glab`q''"

	local k = 1
	foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
		qui summ est if par == "`p'" 
		local m`k' = "`:di %4.3f `r(mean)''"
		qui summ se  if  par == "`p'"
		local s`k' = "(`:di %4.3f `r(mean)'')"
		local ++k 
	}

	*di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
	*di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

	file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
	file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 
}	


file write fh "\bottomrule" _n  
file close fh
macro drop fh
// end of outcome table ----------------------------------------------------------



// outcomes table ----------------------------------------------------------------
// appendix: bins, with contesting 

// initialize LaTeX Table ---------------------
capture erase "${out}/apx_extrap/table_outcomes_bins.tex"
file open fh using "${out}/apx_extrap/table_outcomes_bins.tex", write replace

file write fh ///	
	"&\multicolumn{4}{c}{\$Y_0\$} && \multicolumn{4}{c}{\$Y_1 - Y_0\$}\\" _n /// 
	"\cmidrule(l){2-5} \cmidrule(l){7-10}" _n ///
	"& (1) & (2) & (3) & (4) && (5) & (6) & (7) & (8)\\" _n ///
	"& All & \$D=1\$ & \$D=0\$ & Diff && All & \$D=1\$ & \$D=0\$ & Diff \\" _n ///
	"&     &         &         &      && (ATE) & (ATT) & (ATU) &  \\" _n ///
	"\hline" _n
	

// first panel: any in the next year ---------------
file write fh ///
	"\multicolumn{6}{l}{\textit{\underline{Panel A: Any offense in following year}}}  \\"  _n ///
	
	
// Prep for reading loop -------------
local y1    = "cite_ny1"
local glab1 = "Any Offense"
local y2    = "speed_ny1"
local glab2 = "Speeding"
local y3    = "crash_ny1"
local glab3 = "Crash"

forval q = 1/3 {
	
	import delimited using "${est}/outcomes_b5.csv", clear
	keep if yvar == "`y`q''"
	replace par = trim(upper(par))
	local label = "`glab`q''"

	local k = 1
	foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
		qui summ est if par == "`p'" 
		local m`k' = "`:di %4.3f `r(mean)''"
		qui summ se  if par == "`p'"
		local s`k' = "(`:di %4.3f `r(mean)'')"
		local ++k 
	}

	*di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
	*di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

	file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
	file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 
}	


// second panel: counts over next two years ----------
file write fh "[1em]" _n 
file write fh ///
	"\multicolumn{6}{l}{\textit{\underline{Panel B: Number of offenses in following two years}}}  \\"  _n ///
	

local y1    = "n_cite2"
local glab1 = "Any Offense"
local y2    = "n_speed2"
local glab2 = "Speeding"
local y3    = "n_crash2"
local glab3 = "Crash"


forval q = 1/3 {
	
	import delimited using "${est}/outcomes_b5.csv", clear
	keep if yvar == "`y`q''"
	replace par = trim(upper(par))
	local label = "`glab`q''"

	local k = 1
	foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
		qui summ est if par == "`p'" 
		local m`k' = "`:di %4.3f `r(mean)''"
		qui summ se  if  par == "`p'"
		local s`k' = "(`:di %4.3f `r(mean)'')"
		local ++k 
	}

	*di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
	*di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

	file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
	file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 
}


// second panel: counts over next two years ----------
file write fh "[1em]" _n 
file write fh ///
	"\multicolumn{6}{l}{\textit{\underline{Panel C: Traffic court}}}  \\"  _n ///
	

local y1    = "contest"
local glab1 = "Contest"

forval q = 1/1 {
	
	import delimited using "${est}/outcomes_b5.csv", clear
	keep if yvar == "`y`q''"
	replace par = trim(upper(par))
	local label = "`glab`q''"

	local k = 1
	foreach p in Y0 Y0D1 Y0D0 Y0D1MY0D0 ATE ATT ATU ATTMATU {
		qui summ est if par == "`p'" 
		local m`k' = "`:di %4.3f `r(mean)''"
		qui summ se  if  par == "`p'"
		local s`k' = "(`:di %4.3f `r(mean)'')"
		local ++k 
	}

	*di "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\"
	*di "        & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 

	file write fh "`label' & `m1' & `m2' & `m3' & `m4' && `m5' & `m6' & `m7' & `m8' \\" _n
	file write fh "          & `s1' & `s2' & `s3' & `s4' && `s5' & `s6' & `s7' & `s8' \\" _n 
}	





file write fh "\bottomrule" _n  
file close fh
macro drop fh



// end of outcome table ----------------------------------------------------------












